﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.XPath;

namespace BJYJ_Admin.EntityFramework.Core.DbContexts
{
    public class XmlCommentHelper
    {
        private static XDocument _xmlDocument;

        public static void LoadXmlComments(string xmlFilePath)
        {
            if (File.Exists(xmlFilePath))
            {
                _xmlDocument = XDocument.Load(xmlFilePath);
            }
        }

        public static string GetCommentForProperty(string typeName, string propertyName)
        {
            if (_xmlDocument == null) return null;

            // 构造 XML 注释的成员名称格式，例如：
            // 类的属性：P:命名空间.类名.属性名
            var memberName = $"P:{typeName}.{propertyName}";
            var memberElement = _xmlDocument.XPathSelectElement(
                $"/doc/members/member[@name='{memberName}']");

            if (memberElement == null) return null;

            var summaryElement = memberElement.Element("summary");
            if (summaryElement == null) return null;

            // 清理注释中的换行和空格
            return summaryElement.Value.Trim()
                .Replace("\n", "")
                .Replace("\r", "")
                .Replace("  ", " ");
        }

    }
}
